//
//  AkuDoctorMessage.m
//  KMedicine
//
//  Created by 阿酷 on 16/7/7.
//  Copyright © 2016年 caomu. All rights reserved.
//

#import "AkuDoctorMessage.h"
#import "AkuFocusDoctorModel.h"

@interface AkuDoctorMessage ()<UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>

@property(nonatomic, weak)UIView *topView;

@property (nonatomic,weak) UIView *bottomView;

@property(nonatomic, weak)UIButton *bottomButton;

@property(nonatomic, weak)UIView *centerSelectView;

@property(nonatomic, weak)UIScrollView *centerScrollView;

@property(nonatomic, weak)UIButton *leftButton;

@property(nonatomic, weak)UIButton *rightButton;

@property(nonatomic, weak)UIView *moveSelectView;

@property (nonatomic,weak)UIImageView *iconView;

// topView 控件
@property (nonatomic,weak)UILabel *nameLabel;
@property (nonatomic,weak)UILabel *positionLabel;
@property (nonatomic,weak)UILabel *hospitalLabel;

// topView 的值
@property (nonatomic,copy) NSString *nameStr;
@property (nonatomic,copy) NSString *positionStr;
@property (nonatomic,copy) NSString *hospitalStr;

// stackView
@property (nonatomic, weak) UIStackView *stackView;
@end

static NSString *indentifer = @"cell";
@implementation AkuDoctorMessage

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    // 去除导航栏的偏移量
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.edgesForExtendedLayout = UIRectEdgeBottom | UIRectEdgeLeft | UIRectEdgeRight;
    
    self.view.backgroundColor = [UIColor grayColor];
    
    self.title = @"关注医生";
    
    // 上半部分 的 view
    [self setupUITopView];
    
    // 下面 的 button
    [self setupUIBottomButtom];
    
    // 中间 选择view
    [self setCenterSelectView];
    
    // 中间部分 的 view
    [self setupUICenterScrollView];
    
}

#pragma mark
#pragma mark - 上半部分 topView
- (void)setupUITopView {
    
    // 整体的topView
    UIView *topView = [[UIView alloc] init];
    self.topView = topView;
    topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topView];

    // 头像
    UIImageView *iconImage = [[UIImageView alloc]init];
    iconImage.image = [UIImage imageNamed:@"illness_img_person.png"];
    self.iconView = iconImage;
    [self.topView addSubview:iconImage];
    
    // 姓名
    UILabel *nameLabel = [[UILabel alloc]init];
    nameLabel.text = self.nameStr;
    nameLabel.textColor = [UIColor blackColor];
    nameLabel.font = [UIFont systemFontOfSize:18];
    self.nameLabel = nameLabel;
    [self.topView addSubview:self.nameLabel];
    
    // 职位
    UILabel *positionLabel = [[UILabel alloc]init];
    self.positionLabel = positionLabel;
    positionLabel.text = self.positionStr;
    positionLabel.textColor = [UIColor grayColor];
    positionLabel.font = [UIFont systemFontOfSize:17];
    [self.topView addSubview:self.positionLabel];

    // 就职医院
    UILabel *hospitalLabel = [[UILabel alloc]init];
    hospitalLabel.text = self.hospitalStr;
    hospitalLabel.textColor = [UIColor grayColor];
    hospitalLabel.font = [UIFont systemFontOfSize:17];
    self.hospitalLabel = hospitalLabel;
    [self.topView addSubview:self.hospitalLabel];
    
    // stackView
    UIStackView *stackView = [[UIStackView alloc] init];
    self.stackView = stackView;
    stackView.backgroundColor = [UIColor redColor];
    stackView.distribution = UIStackViewDistributionFillEqually;
    [self.topView addSubview:stackView];
    
    // 设置 锦旗 预约量 鲜花 button
    [stackView addArrangedSubview:[self setButtonWithImgStr:@"yuyueliang"]];
    [stackView addArrangedSubview:[self setButtonWithImgStr:@"xianhua"]];
    [stackView addArrangedSubview:[self setButtonWithImgStr:@"jinqi"]];
    
    [self setTopViewMasonry];
}

// 设置 button
- (UIButton *)setButtonWithImgStr:(NSString *)imgStr {
    UIButton *button = [[UIButton alloc] init];
    
    [button setTitle:[NSString stringWithFormat:@"%d", arc4random_uniform(100)] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    
    [button setTitleColor:kThemeColor forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:imgStr] forState:UIControlStateNormal];
    
    return button;
    
}

// 添加约束
- (void)setTopViewMasonry{

    // topView
    [self.topView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.height.offset(120);
    }];
    
    // 头像
    [self.iconView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20);
        make.bottom.equalTo(self.stackView.top).offset(-5);
        make.width.offset = 80;
        make.height.offset = 80;
    }];
    
    // 名字 label
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconView.mas_top).offset(10);
        make.left.equalTo(self.iconView.mas_right).offset(10);
    }];
    
    // 职位 label positionLabel
    [self.positionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_top);
        make.left.equalTo(self.nameLabel.mas_right).offset(15);
    }];
    
    // 医院 label hospitalLabel
    [self.hospitalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.iconView.mas_bottom).offset(-10);
        make.left.equalTo(self.nameLabel.mas_left);
    }];
    
    // stackView
    [self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-5);
        make.height.offset(20);
        make.left.equalTo(20);
        make.width.offset(320);
    }];
    
    
}



#pragma mark
#pragma mark - topView上的label赋值
- (void)setFocusDoctorCellmodel:(AkuFocusDoctorModel *)focusDoctorCellmodel {
    
    _focusDoctorCellmodel = focusDoctorCellmodel;
    
    self.nameStr = focusDoctorCellmodel.name;
    self.positionStr = focusDoctorCellmodel.position;
    self.hospitalStr = focusDoctorCellmodel.hospital;
    
}

#pragma mark
#pragma mark - 下面button - 咨询医生按钮
- (void)setupUIBottomButtom {
    
    UIView *bottomView = [[UIView alloc]init];
    self.bottomView = bottomView;
    [bottomView setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:self.bottomView];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(60);
        make.right.bottom.offset(0);
        make.left.offset(0);
    }];
    
    UIButton *bottomButton = [[UIButton alloc] init];
    
    self.bottomButton = bottomButton;
    
    bottomButton.layer.cornerRadius = 10;
    bottomButton.layer.masksToBounds = YES;
    
    [bottomButton setBackgroundColor:kThemeColor];
    [bottomButton setTitle:@"资讯医生" forState:UIControlStateNormal];
    
    [self.bottomView addSubview:bottomButton];
    
    [bottomButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(self.bottomView.mas_height).offset(-10);
        make.right.bottom.offset(-10);
        make.left.offset(10);
    }];
}

#pragma mark
#pragma mark - 中间 选择view 按钮View
- (void)setCenterSelectView {
    
    UIView *centerSelectView = [[UIView alloc] init];
    
    self.centerSelectView = centerSelectView;
    
    centerSelectView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:centerSelectView];
    
    [centerSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.offset(0);
        make.top.equalTo(self.topView.bottom).offset(2);
        make.height.offset(64);
    }];
    
    
    UIButton *leftButton = [[UIButton alloc] init];
    UIButton *rightButton = [[UIButton alloc] init];
    
    self.leftButton = leftButton;
    self.rightButton = rightButton;
    
    self.leftButton.selected = YES;
    self.rightButton.selected = NO;

    [leftButton setTitle:@"就诊条件" forState:UIControlStateNormal];
    [rightButton setTitle:@"医生简介" forState:UIControlStateNormal];
    
    leftButton.backgroundColor = [UIColor whiteColor];
    rightButton.backgroundColor = [UIColor whiteColor];
    
    [leftButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    [leftButton setTitleColor:kThemeColor forState:UIControlStateSelected];
    [rightButton setTitleColor:kThemeColor forState:UIControlStateSelected];
    
    [centerSelectView addSubview:leftButton];
    [centerSelectView addSubview:rightButton];
    
    [leftButton addTarget:self action:@selector(didLeftButton) forControlEvents:UIControlEventTouchUpInside];
    [rightButton addTarget:self action:@selector(didRightButton) forControlEvents:UIControlEventTouchUpInside];
    
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(centerSelectView.centerY);
        make.centerX.equalTo(centerSelectView.centerX).multipliedBy(0.5);
        make.height.offset(40);
        make.width.offset(80);
    }];
    
    
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(centerSelectView.centerY);
        make.centerX.equalTo(centerSelectView.centerX).multipliedBy(1.5);
        make.height.offset(40);
        make.width.offset(80);
    }];
    
    // 移动 的 到当前 选择 的view - 下划线
    UIView *moveSelectView = [[UIView alloc] init];
    self.moveSelectView = moveSelectView;
    
    moveSelectView.backgroundColor = [UIColor purpleColor];
    
    [centerSelectView addSubview:moveSelectView];
    
    [moveSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.offset(0);
        make.centerX.equalTo(leftButton.centerX);
        make.width.equalTo(leftButton.width);
        make.height.offset(2);
    }];
    
}



#pragma mark
#pragma mark - 中间部分 的 CenterScrollView
- (void)setupUICenterScrollView {
    
    UIScrollView *centerScrollView = [[UIScrollView alloc] init];
    
    self.centerScrollView = centerScrollView;
    
    centerScrollView.delegate = self;
    
    centerScrollView.pagingEnabled = YES;
    centerScrollView.showsHorizontalScrollIndicator = NO;
    
    
    [self.view addSubview:centerScrollView];
    
    [centerScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.centerSelectView.mas_bottom);
        make.left.right.offset(0);
        make.bottom.equalTo(self.bottomView.mas_top).offset(0);
    }];
    
    UITableView *tableView = [[UITableView alloc] init];
    
    tableView.backgroundColor = [UIColor greenColor];
    
    tableView.dataSource = self;
    tableView.delegate = self;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    tableView.tag = 1;
    tableView.bounces = NO;
    
    [centerScrollView addSubview:tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.bottom.offset(0);
        make.width.equalTo(centerScrollView).multipliedBy(1);
        make.height.equalTo(centerScrollView).multipliedBy(1);
    }];
    
    UITableView *tableView2 = [[UITableView alloc] init];
    
    tableView2.backgroundColor = [UIColor yellowColor];
    
    tableView2.dataSource = self;
    tableView2.delegate = self;
    [tableView2 registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell2"];
    tableView2.tag = 2;
    tableView2.bounces = NO;
    [centerScrollView addSubview:tableView2];
    
    [tableView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.right.bottom.offset(0);
        make.left.equalTo(tableView.mas_right);
        make.width.equalTo(centerScrollView).multipliedBy(1);
        make.height.equalTo(centerScrollView).multipliedBy(1);
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView.tag == 1) {
        indentifer = @"cell";
    }else{
    
        indentifer = @"cell2";
    }
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifer forIndexPath:indexPath];
    
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifer];
    }
    
    if ([indentifer isEqualToString:@"cell"]) {
        cell.textLabel.text = [NSString stringWithFormat:@"就诊条件_>%ld", indexPath.row];
    }else{
        cell.textLabel.text = [NSString stringWithFormat:@"医生简介_>%ld", indexPath.row];
    }
    
    cell.backgroundColor = [UIColor whiteColor];
    
    return cell;
}

// 手指滑动的时候
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
}


- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    
    NSLog(@"只有 手指 点击button 才调用");
}

// 减速 将要停止时调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    NSLog(@"减速");
    // 判断 是 滑动 了scrollView
    if ([scrollView isEqual:self.centerScrollView]) {
        
        // 滑动到 左边 按钮
        if (scrollView.contentOffset.x < scrollView.bounds.size.width / 2) {
            NSLog(@"左边");
            [self didLeftButton];
            
            
        } else if (scrollView.contentOffset.x > scrollView.bounds.size.width / 2 ) {
            // 滑动 到 右边 按钮
            NSLog(@"右边");
            [self didRightButton];
        }
        
    }

    
    
}




// 点击了左边的 就诊条件按钮
- (void)didLeftButton {
    
    self.leftButton.selected = YES;
    self.rightButton.selected = NO;
    
    CGFloat moveX = self.view.bounds.size.width * 1/4;
    
    [self.centerScrollView setContentOffset:CGPointZero animated:YES];
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.moveSelectView.center = CGPointMake(moveX, self.moveSelectView.center.y);
    }];
}
// 点击了右边的 医生简介按钮
- (void)didRightButton {
    
    self.leftButton.selected = NO;
    self.rightButton.selected = YES;
    
    CGFloat moveX = self.view.bounds.size.width * 3/4;
    
    [self.centerScrollView setContentOffset:CGPointMake(self.view.bounds.size.width, 0) animated:YES];
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.moveSelectView.center = CGPointMake(moveX, self.moveSelectView.center.y);
    }];
}


@end



